package com.heima.article.controller;

import com.heima.article.service.ApArticleService;
import com.heima.article.service.impl.ApArticleServiceImpl;
import com.heima.common.dtos.ResponseResult;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.media.dtos.WmNewsResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ApArticleController {

    @Autowired
    private ApArticleService apArticleService;
    /**
     * 保存文章
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/article/save")
    public Long saveArticle(@RequestBody WmNewsResultDTO dto){
        return apArticleService.saveArticle(dto);
    }

    /**
     * app查询首页
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/article/load")
    public ResponseResult<List<ArticleDto>> loadHome(@RequestBody ArticleHomeDto dto){
        return ResponseResult.ok(apArticleService.loadArticle(dto,2));
    }

    /**
     * app查询更多
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/article/loadmore")
    public ResponseResult<List<ArticleDto>> loadMore(@RequestBody ArticleHomeDto dto){
        return ResponseResult.ok(apArticleService.loadArticle(dto,2));
    }

    /**
     * app查询最新
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/article/loadnew")
    public ResponseResult<List<ArticleDto>> loadNew(@RequestBody ArticleHomeDto dto){
        return ResponseResult.ok(apArticleService.loadArticle(dto,1));
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/api/v1/article/findByPage")
    public List<ArticleDto> findByPage(@RequestParam(name = "page",defaultValue = "1")Integer page,
                                       @RequestParam(name = "size",defaultValue = "10")Integer size){
        return apArticleService.findByPage(page,size);
    }

    /**
     * 根据文章id 查询文章对象
     * @param articleId
     * @return
     */
    @GetMapping("/api/v1/article/getByArticleId/{id}")
    public ArticleDto getByArticleId(@PathVariable(name = "id")Long articleId){
        return apArticleService.getByArticleId(articleId);
    }
}
